﻿using KBWebApi.Common;
using KBWebApi.Model;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Threading.Tasks;

namespace KBWebApi.Dal
{
    public class WorkStatusDal
    {
        private static SqlSugarScope db = SqlDBHelper.SqlHelper.GetDB();

        public static bool IsExists(WorkStatusModel workStatus)
        {
            try
            {
                bool isexists = db.Queryable<WorkStatusModel>().Any(t => t.XS_WorkStatusId == workStatus.XS_WorkStatusId);
                return isexists;
            }
            catch (Exception ex)
            {
                WriteLog.Writelog("WorkStatus.IsExists=>" + ex.ToString());
                return false;
            }
        }

        public int NewKBInput(WorkStatusModel workStatus)
        {
            int count = db.Insertable(workStatus).ExecuteCommand();
            return count;
        }

        public int Update(WorkStatusModel workStatus)
        {
            int count = db.Updateable(workStatus).IgnoreColumns(t => new { t.CreateDate }).ExecuteCommand();
            return count;
        }

        public int Delete(string WorkStatusId)
        {
            int count = db.Deleteable<WorkStatusModel>().Where(t => t.XS_WorkStatusId == int.Parse(WorkStatusId)).ExecuteCommand();
            return count;
        }

        public static DataTable GetAllList()
        {
            return db.Queryable<WorkStatusModel>()
            .With(SqlWith.NoLock)
            .OrderBy(t => new { t.XS_WorkStatusId, t.CreateDate })
            .ToDataTable();
        }
    }
}
